package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Returnsupplier;
import com.li.drug.service.ReturnsupplierService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 退货给供应商记录相关controller
 */
@Controller
@RequestMapping("/returnsupplier")
public class ReturnsupplierController {

    @Autowired
    ReturnsupplierService returnsupplierService;

    /**
     * 转向退货给供应商记录页面
     * @return
     */
    @RequestMapping
    public String returnsupplier(){
        return "returnsupplier/returnsupplier";
    }

    //分页查询退货给供应商记录列表
    @RequestMapping("/returnsupplierQueryPage")
    @ResponseBody
    public Object returnsupplierQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum,
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Returnsupplier> page = returnsupplierService.selectReturnsupplierPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向退货给供应商记录新增页面
     * @return
     */
    @RequestMapping("/returnsupplierPage")
    public String returnsupplierPage(){
        return "returnsupplier/returnsupplierPage";
    }

    /**
     * 添加一个退货给供应商记录
     */
    @RequestMapping("/returnsupplierAdd")
    @ResponseBody
    public Object returnsupplier(Returnsupplier returnsupplier){
        returnsupplier.setCreatetime(new Date());
        int i = returnsupplierService.addReturnsupplier(returnsupplier);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向退货给供应商记录编辑页面
     * @return
     */
    @RequestMapping("/returnsupplierQueryById")
    public String returnsupplierEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Returnsupplier returnsupplier = returnsupplierService.getReturnsupplierById(id);
        model.addAttribute("obj",returnsupplier);
        return "returnsupplier/returnsupplierPage";
    }

    /**
     * 修改一个退货给供应商记录信息
     */
    @RequestMapping("/returnsupplierEdit")
    @ResponseBody
    public Object returnsupplierEdit(Returnsupplier returnsupplier){
        try {
            returnsupplier.setCreatetime(new Date());
            int i = returnsupplierService.editReturnsupplier(returnsupplier);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个退货给供应商记录信息
     */
    @RequestMapping("/returnsupplierDelById")
    @ResponseBody
    public Object returnsupplierDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = returnsupplierService.delReturnsupplierById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有退货给供应商记录列表
     */
    @RequestMapping("/returnsupplierList")
    @ResponseBody
    public Object returnsupplierList(){
        List<Returnsupplier> list = returnsupplierService.list();
        return list;
    }
}
